package com.beilang.hotline.company.page;

import java.util.Date;
import java.util.List;

import com.beilang.base.codelookup.manager.CodeLookupMgr;
import com.beilang.base.core.page.BasePage;
import com.beilang.base.pagination.PaginationHelper;
import com.beilang.base.util.DateUtil;
import com.beilang.base.util.CodeConstant;
import com.beilang.base.util.StringUtil;
import com.beilang.hotline.company.manager.CompanyMgr;
import com.beilang.hotline.company.model.CompanyModel;
import com.beilang.hotline.company.to.SearchTO;
import com.beilang.hotline.user.model.UserModel;

public class CompanyPage extends BasePage {
	
	private CompanyMgr companyMgr;
	
	private CodeLookupMgr codeLookupMgr;
	
	public void setCodeLookupMgr(CodeLookupMgr codeLookupMgr) {
		this.codeLookupMgr = codeLookupMgr;
	}

	public void setCompanyMgr(CompanyMgr companyMgr) {
		this.companyMgr = companyMgr;
	}
	
	public List getAddCustomAreaList(){
		return codeLookupMgr.getCodeList(CodeConstant.CODE_CUSTOM_AREA_LIST);
	}
	
	public List getAddServiceLevelList(){
		return codeLookupMgr.getCodeList(CodeConstant.CODE_SERVICE_LEVEL_LIST);
	}

	public List getUpdateCustomAreaList(){
		return this.getAddCustomAreaList();
	}

	public List getupdateServiceLevelList(){
		return this.getAddServiceLevelList();
	}	    

    public PaginationHelper getSearchList(PaginationHelper helper,SearchTO searchTO){
		helper=companyMgr.getCompanyList(helper, searchTO);
		return helper;
	}
	
	public PaginationHelper deleteCompanyList(PaginationHelper helper,SearchTO searchTO,List deleteList){
        for (Object aDeleteList : deleteList) {
            CompanyModel companyModel = (CompanyModel) aDeleteList;
            companyMgr.deleteCompany(companyModel.getCompanyId());
        }
		return this.getSearchList(helper, searchTO);
	}
	
	public CompanyModel addCompany(CompanyModel companyModel){
		UserModel user=this.getLoginUser();
		companyModel.setCreateId(user.getUserId());
		companyModel.setUpdateId(user.getUserId());
		Date date = DateUtil.getDate();
		companyModel.setCreateTime(date);
		companyModel.setUpdateTime(date);		
		companyModel=companyMgr.createCompany(companyModel);
		return companyModel;
	}
	
	public CompanyModel getViewCompany(Long companyId){
		return companyMgr.getCompany(companyId);
    }
    public CompanyModel getViewCompanyWithC(Long companyId){
		CompanyModel companyModel = companyMgr.getCompany(companyId);
        if (companyModel!=null)
        {
            companyModel.setCompanyName(StringUtil.splitString(companyModel.getCompanyName()));
            companyModel.setCustomAreaDesc(StringUtil.splitString(companyModel.getCustomAreaDesc()));
        }
        return companyModel;
    }
	
    public CompanyModel getUpdateCompany(Long companyId){
		return getViewCompany(companyId);
	}
	
	public CompanyModel updateCompany(CompanyModel companyModel){
		UserModel user=this.getLoginUser();
		companyModel.setUpdateId(user.getUserId());
		Date date = DateUtil.getDate();
		companyModel.setUpdateTime(date);		
		companyModel=companyMgr.saveCompany(companyModel);
		return companyModel;
	}	
	
}
